package cn.itcast.controller;

import cn.itcast.common.utils.Page;
import cn.itcast.pojo.BaseDict;
import cn.itcast.pojo.Customer;
import cn.itcast.pojo.User;
import cn.itcast.service.BaseDictService;
import cn.itcast.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 客户管理控制器
 */
@Controller
public class CustomerController {

    //依赖注入
    @Autowired
    private CustomerService customerService;
    @Autowired
    private BaseDictService baseDictService;

    //客户来源
    @Value("${customer.from.type}")
    private String from_type;
    //客户行业
    @Value("${customer.industry.type}")
    public String industry_type;
    //客户级别
    @Value("${customer.level.type}")
    public String level_type;

    //客户列表
    @RequestMapping("/customer/list.action")
    //在SpringMvc后台进行获取数据，一般是两种。1.request.getParameter(“参数名”)2.用@RequestParam注解获取
    public String customerlist(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,
                               String custName, String customerSource, String customerIndustry,
                               String customerlevel, Model model){
        //条件查询所有客户
        Page<Customer> customerPage = customerService.findCustomerList(page,rows,custName,customerSource,customerIndustry,customerlevel);
        model.addAttribute("page",customerPage);

        //客户来源
        List<BaseDict> fromType = baseDictService.findBaseDictByTypeCode(from_type);
        //客户行业
        List<BaseDict> industryType = baseDictService.findBaseDictByTypeCode(industry_type);
        //客户级别
        List<BaseDict> levelType = baseDictService.findBaseDictByTypeCode(level_type);

        //添加参数
        model.addAttribute("fromType",fromType);
        model.addAttribute("industryType",industryType);
        model.addAttribute("levelType",levelType);
        model.addAttribute("custName",custName);
        model.addAttribute("custSource",customerSource);
        model.addAttribute("custIndustry",customerIndustry);
        model.addAttribute("custLevel",customerlevel);

        return "customer";
    }

    //创建客户
    @RequestMapping("/customer/create.action")
    @ResponseBody  //用于直接返回return对象，该注解用在方法上
    public String CustomerCreate(Customer customer, HttpSession session){

        //获取session中的用户信息
        User user = (User) session.getAttribute("USER_SESSION");

        //将当前的用户ID存储在客户对象中
        customer.setCustCreateId(user.getUserId());

        //创建DATA对象
        Date date = new Date();

        //得到一个时间，存入mysql中
        Timestamp timestamp = new Timestamp(date.getTime());
        customer.setCustCreatetime(timestamp);

        //执行service中创建方法，返回的是搜影响的行数
        int rows = customerService.createCustomer(customer);



        if (rows>0){
            return "OK";
        }else{
            return "fail";
        }
    }

    //通过ID获取用户
    @RequestMapping("/customer/getCustomerById.action")
    @ResponseBody
    public Customer customerUpdate(Integer id){

        Customer customer = customerService.findCustomweById(id);

        return customer;
    }
    //跟新用户
    @RequestMapping("/customer/update.action")
    @ResponseBody
    public String updateCustomer(Customer customer){

        int rows = customerService.updateCustomer(customer);

        if(rows>0){
            return "OK";
        }else {
            return "FAIL";
        }

    }
    //删除用户
    @RequestMapping("customer/delete.action")
    @ResponseBody
    public String delateCustomer(Integer id){

        int rows = customerService.delateCustomer(id);
        if(rows>0){
            return "OK";
        }else {
            return "FAIL";
        }
    }

}
